/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package triseries;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jdom.*;

/**
 * @author tshepo
 * 
 */
public class BTSubtitles {

	private static final boolean DEBUG = (Main.debug || MyProperties.debug.equalsIgnoreCase("BTSubtitles"));
	private String filename;
	private String url;
	private int quality;
	private List<String> content;
	private String bestMatchItem;
	private static String filtreAvec;
	private static String filtreSans;

	public static void setFiltres(String fAvec, String fSans) {
		filtreAvec = fAvec;
		filtreSans = fSans;
	}

	
	public BTSubtitles(Element Subs) {

		if(DEBUG) System.err.println("BTSubtitles()");
		
			content = new ArrayList<String>();
			this.filename = Subs.getChild("file").getText();
			this.url = Subs.getChild("url").getText();
			if (!filename.isEmpty() && Subs.getChild("content") != null) {
				List<?> listElements = Subs.getChild("content")
						.getChildren("item");
				Iterator<?> i = listElements.iterator();
				while(i.hasNext()) {
					content.add(((Element)i.next()).getText().toString());
				}
			}
			quality = Integer.parseInt(Subs.getChild("quality").getText());

	}

	public int Quality() {
		return quality;
	}

	public int bestMatch(String[] attributes, String s, String e) {
		if(DEBUG) System.err.println("BTSubtitles.bestMatch("+attributes.toString()+","+s+","+e+")");
		int bestval = 0;
		for (String item : content) {
			if (Pattern.matches(".*(" + s + ").*(" + e + ").*", item)
					&& Pattern.matches(filtreAvec, item)
					&& !Pattern.matches(filtreSans, item)) {
				int val = quality;
				for (String att : attributes) {
					Matcher m = Pattern.compile(att, Pattern.CASE_INSENSITIVE)
							.matcher(item);
					while (m.find()) {
						val++;
					}
				}
				if (val >= bestval) {
					bestval = val;
					bestMatchItem = item;
				}
			}
		}
		return bestval;
	}

	@Override
	public String toString() {
		if(DEBUG) System.err.println("BTSubtitles.toString()");
		String returnString = filename + " Quality = "
				+ String.valueOf(quality);
		for (String s : content) {
			returnString += "\n - " + s;
		}
		return returnString;
	}

	public String bestSubtitle() {
		return bestMatchItem;
	}

	public String getUrl() {
		return url;
	}

	public String getFile() {
		return filename;
	}
}
